<template>
  <section id="myChart" ref="myChart"></section>
</template>
<script>
import * as echarts from 'echarts'
export default {
  name: 'BarChart',
  components: {},
  props: {
    chartData: {
      type: Array,
      default: () => []
    }
  },
  data () {
    return {
      chart: null
    }
  },
  computed: {
    option () {
      return {
        backgroundColor: '#00265f',
        "title": {
          "text": "",
          x: "center",
          y: "4%",
          textStyle: {
            color: '#fff',
            fontSize: '22'
          },
          subtextStyle: {
            color: '#90979c',
            fontSize: '16',

          },
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow'
          }
        },
        grid: {
          top: '25%',
          right: '5%',
          left: '15%',
          bottom: '10%'
        },
        xAxis: [{
          type: 'category',
          data: this.chartData.map(item => item.name),
          axisLine: {
            lineStyle: {
              color: 'rgba(255,255,255,0.12)'
            }
          },
          axisLabel: {
            margin: 10,
            color: '#e2e9ff',
            textStyle: {
              fontSize: 14
            },
          },
        }],
        yAxis: [{

          axisLabel: {
            formatter: '{value}',
            color: '#e2e9ff',
          },
          axisLine: {
            show: false,
            lineStyle: {
              color: 'rgba(255,255,255,1)'
            }
          },
          splitLine: {
            lineStyle: {
              color: 'rgba(255,255,255,0.12)'
            }
          }
        }],
        series: [{
          type: 'bar',
          data: this.chartData.map(item => item.value),
          barWidth: '20px',
          itemStyle: {
            normal: {
              color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
                offset: 0,
                color: 'rgba(0,244,255,1)' // 0% 处的颜色
              }, {
                offset: 1,
                color: 'rgba(0,77,167,1)' // 100% 处的颜色
              }], false),
              barBorderRadius: [30, 30, 30, 30],
              shadowColor: 'rgba(0,160,221,1)',
              shadowBlur: 4,
            }
          },
          label: {
            normal: {
              show: true,
              lineHeight: 30,
              height: 30,
              backgroundColor: 'rgba(0,160,221,0.1)',
              borderRadius: 200,
              position: ['0', '-30'],
              distance: 1,
              formatter: (params) => params.value,
            }
          }
        }]
      };
    }
  },
  watch: {
    chartData: {
      handler () {
        this.drawLine()
      },
      deep: true
    }
  },
  created () {

  },
  mounted () {
    this.drawLine()
  },
  methods: {
    async drawLine () {
      await this.$nextTick()
      this.chart = this.$echarts.init(this.$refs.myChart)
      this.chart.setOption(this.option);
    },
    handleResize () {
      this.chart && this.chart.resize()
    }
  }
}
</script>
<style scoped lang='scss'>
#myChart {
  width: 100%;
  height: 100%;
}
</style>